//
//  HomeVc.swift
//  QQZone
//
//  Created by xmg on 16/9/1.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

class HomeVc: UIViewController {

    /** 设置边栏内容 */
    private lazy var dock : Dock = Dock()
    /** 设置容器内容 */
    private lazy var  containerV : UIView = UIView()
    /** 记录点击 */
    private lazy var currentIndex : Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
//        view.backgroundColor = UIColor.orangeColor()
        setUpDock()
        setUpContainer()
        
        preferredStatusBarStyle()
        
        topVItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
      
    }
}
extension HomeVc{

    /** 设置边栏 */
  private func setUpDock(){
    /** 添加dockView */
    view.addSubview(dock)
    let isBool = view.frame.width > view.frame.height
    let dockW = isBool ? kCrossScreenW : kVerticalScreenW
    dock.frame = CGRect(x: 0, y: 0, width: dockW, height: view.frame.height)
    dock.setUpScreenDirection(isBool)
    /** 设置顶部 */
    dock.topV.setUpScreenDirection(isBool)
    dock.topV.setTitle("小明🐘", forState: .Normal)
    dock.topV.setImage(UIImage(named: "hong"), forState: .Normal)
    dock.topV.addTarget(self, action: "topVItem", forControlEvents: .TouchUpInside)
    /** 设置代理 */
    dock.middleV.delegate = self
    dock.bottomV.delegate = self
    }
    
   @objc private func topVItem(){
    
        middleView(dock.middleV, index: childViewControllers.count - 1)
    
        dock.middleV.selectItem?.selected = false
    }
    /** 监听屏幕的旋转 */
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        let isBool = size.width > size.height
        UIView.animateWithDuration(coordinator.transitionDuration()) { () -> Void in
            self.dock.frame.size.width = isBool ? kCrossScreenW : kVerticalScreenW
            self.dock.frame.size.height = size.height
            self.dock.setUpScreenDirection(isBool)
            /** 重新设置容器 */
            let containerW :CGFloat = isBool ?  (size.width - (self.dock.frame.width +  self.containerV.frame.width)) * 0.5 : 0
            
            self.containerV.frame.origin.x = self.dock.frame.width + containerW
            self.containerV.frame.size.height = size.height - kStatusBarH
        }
    }
    
    }

extension HomeVc : MiddleViewDelegate,BottomViewDelegate{

    func middleView(middleV: MiddleView, index: Int) {
        /** 如果控制重复点击就返回 */
        if currentIndex == index {return}
        /** 获取旧的视图并且移除 */
        let oldVc = childViewControllers[currentIndex]
        oldVc.view.removeFromSuperview()
        /** 获取新的视图 */
        let newVc = childViewControllers[index]
        /** 设置视图 */
        newVc.view.frame = containerV.bounds
        /** 添加视图 */
        containerV.addSubview(newVc.view)
        /** 重新赋值 */
        currentIndex = index
    }
    func bottomMenu(bottomV: bottomView, type: Int) {
        
        switch type {
        case 0:
            /** 创建发表心情 */
            let  moodVc = ExpressMoodVC()
            /** 包装一个nav  */
            let moodNav = UINavigationController(rootViewController: moodVc)
            /** 设置弹出样式 */
//            moodNav.modalTransitionStyle = .FlipHorizontal 水平翻转
            moodNav.modalPresentationStyle = .FormSheet
            presentViewController(moodNav, animated: true, completion: nil )
             print("发表心情");
        case 1:
             print("点击了相片");
        case 2:
             print("点击了日志");
        default :
            print("其他");
        }
    }
}

extension HomeVc {

    /** 设置容器View */
    private func setUpContainer(){
    
        view.addSubview( containerV)
        let isBool = view.frame.width > view.frame.height
        let minW = min(view.frame.height, view.frame.width)
        let containerW = minW - kVerticalScreenW
        let containerX = isBool ? ((UIScreen.mainScreen().bounds.width - (self.dock.frame.width + containerW)) * 0.5) + self.dock.frame.width : dock.frame.width
        containerV.backgroundColor = UIColor.lightGrayColor()
        
        containerV.frame = CGRect(x: containerX, y: kStatusBarH, width: containerW, height: view.frame.height - kStatusBarH)
        setUpChildContainer()
    }
    /** 初始化所有的子控制器 */
    private func setUpChildContainer(){
    
        addChildVc(AllDynamicVC(), title: "全部动态", bgColor: UIColor.orangeColor())
        addChildVc(UIViewController(), title: "与我相关", bgColor: UIColor.greenColor())
        addChildVc(UIViewController(), title: "照片墙", bgColor: UIColor.purpleColor())
        addChildVc(UIViewController(), title: "电子相框", bgColor: UIColor.yellowColor())
        addChildVc(UIViewController(), title: "好友", bgColor: UIColor.redColor())
        addChildVc(UIViewController(), title: "更多", bgColor: UIColor.brownColor())
        addChildVc(UIViewController(), title: "个人中心", bgColor: UIColor.whiteColor())
    }
    
    private func addChildVc(vc : UIViewController ,title : String ,bgColor : UIColor){
        let nav = UINavigationController(rootViewController: vc)
        vc.title = title
        vc.view.backgroundColor = bgColor
        addChildViewController(nav)
    }

}
